﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace ResearchId.Validation
{
    public partial class Demographic : System.Web.UI.Page
    {
        private const int _groupId = 1;
        private const ClaimEnum _claimId_Gender = ClaimEnum.Gender;
        private const string _gender_datatype = "System.String";
        private const ClaimEnum _claimId_Birthdate = ClaimEnum.Birthdate;
        private const string _birthdate_datatype = "System.DateTime";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            Group myGroup = Group.GetGroup(_groupId);
            foreach (GroupClaim myClaim in myGroup.GroupClaims)
            {
                IndividualClaim myIndividualClaim = IndividualClaim.GetIndividualClaim(
                    (ClaimEnum)myClaim.ClaimId
                    , Individual.GetIndividual(Page.User.Identity.Name).IndividualId);
                if (myIndividualClaim != null)
                {
                    switch ((ClaimEnum)myIndividualClaim.ClaimId)
                    {
                        case _claimId_Birthdate:
                            DateTime BirthDate = DateTime.Parse(myIndividualClaim.Value);
                            BindBirthdate(BirthDate);
                            break;
                        case _claimId_Gender:
                            BindGender(myIndividualClaim.Value);
                            break;
                    }
                }
            }
        }
        private void BindBirthdate(DateTime birthdate)
        {
            ddlDay.Items.FindByValue(birthdate.Day.ToString()).Selected = true; ;
            ddlMonth.Items.FindByValue(birthdate.Month.ToString()).Selected = true ;
            ddlYear.Items.FindByValue(birthdate.Year.ToString()).Selected = true ;

        }
        private void BindGender(string Gender)
        {
            ddlGender.Items.FindByText(Gender).Selected = true;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string UserName = Page.User.Identity.Name;
            Individual i = Individual.GetIndividual(UserName);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_Gender
                , ddlGender.SelectedItem.Text
                , _gender_datatype);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_Birthdate
                , new DateTime(
                    Int32.Parse(ddlYear.SelectedItem.Value)
                    , Int32.Parse(ddlMonth.SelectedItem.Value)
                    , Int32.Parse(ddlDay.SelectedItem.Value)).ToString()
                , _birthdate_datatype);            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("..\\default.aspx");
        }
    }
}
